package com.lhy.admin.config;

import com.lhy.admin.common.secure.filter.AuthenticationInterceptor;
import com.lhy.admin.common.secure.filter.AuthorizationInterceptor;
import com.lhy.admin.common.secure.filter.CrosFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    /**
     * {@inheritDoc}
     * </br>注册 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthenticationInterceptor());
        registry.addInterceptor(new AuthorizationInterceptor());
    }

    @Bean
    public FilterRegistrationBean crosFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new CrosFilter());
        Map<String, String> pa = new HashMap<>();
        pa.put("allowOrigin", "http://localhost:3000");
        registration.setInitParameters(pa);
        registration.addUrlPatterns("/*");
        return registration;
    }

}
